//
//  LPActivityDetailDetailsTableViewCell.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 2017/6/6.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPActivityDetailDetailsTableViewCell.h"
#import "LPActivityDetailsPastTableViewCell.h"
#import "LPActivityDetailDetailsHeaderView.h"
#import "LPActivityDetailTableFooterView.h"
#import "LPActDetailResouceTableViewCell.h"
#define RECOUCECELL @"recouseCell"
#define PASTCELL @"pastcell"
#define NODATA @"nodata"
#import "LPActivityDetailStudyNoDataTableViewCell.h"
#import "LPActivityDetailDetailsFooterView.h"
#import "ZJStudyRecommendModel.h"
@interface LPActivityDetailDetailsTableViewCell ()<UITableViewDelegate,UITableViewDataSource,LPActivityDetailFooterDelegate,LPActivityDetailDetailsFooterStudyDelegate,LPActivityDetailDetailsHeaderViewDelgate>
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;
/**  tablegaodu  */
@property (assign, nonatomic) CGFloat  tableHeight;
/**  所有数据  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;

@end

@implementation LPActivityDetailDetailsTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
         [self layoutUI];
    }
    return self;
}


- (void)setModelArray:(NSArray *)modelArray{
    _modelArray = modelArray;
    
    CGFloat resouceHeight = 0.0;
    /**  活动资源的高度  */
    if (self.resouceArray.count != 0) {
        resouceHeight = (self.resouceArray.count + 2) * 50 * [UIScreen zj_screenHScale];
    }
    /**  往期回顾和header的高度  */
    if (modelArray.count == 0) {
        self.tableHeight =  80 * [UIScreen zj_screenHScale] + 190 * [UIScreen zj_screenHScale] + self.inforModel.NAMEHEIGHT + self.inforModel.HEIGHT + 240 * [UIScreen zj_screenHScale] + resouceHeight;
        
        
    }else{
        
        self.tableHeight =   120 * [UIScreen zj_screenHScale] * modelArray.count  + 190 * [UIScreen zj_screenHScale] + self.inforModel.NAMEHEIGHT + self.inforModel.HEIGHT + 240 * [UIScreen zj_screenHScale] + resouceHeight;
        
    }
    
    
    /**  学习的高度  */
    CGFloat footerHeight = 0.0;
    
    for (ZJStudyRecommendModel *model in self.studyArray) {
        if ([model.COURSETYPE isEqualToString:@"4"]){//悦读会
            footerHeight += 125 * [UIScreen zj_screenHScale];//图书
        }else{
            footerHeight += 120 * [UIScreen zj_screenHScale];
            
        }

    }
    
    if (self.studyArray.count != 0) {
        LPActivityDetailDetailsFooterView *footer = [[LPActivityDetailDetailsFooterView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], footerHeight + 110 * [UIScreen zj_screenHScale])];
        footer.delegate = self;
        footer.studyArray = self.studyArray;
        self.tableView.tableFooterView = footer;
        self.tableHeight += 110 * [UIScreen zj_screenHScale];
    }
    
    self.tableHeight += footerHeight ;
    
    
    if (self.resouceArray.count != 0) {
        [self.dataSouce addObject:@{@"title": @"活动资源", @"data": self.resouceArray}];
    }
    
   
    [self.dataSouce addObject:@{@"title": @"往期回顾", @"data": modelArray}];
    CGRect frame = self.tableView.frame;
    frame.size.height = self.tableHeight;
    self.tableView.frame = frame;
    [self.tableView reloadData];
   
    [self addHeader];
    
}


- (void)addHeader{
    LPActivityDetailDetailsHeaderView *header = [[LPActivityDetailDetailsHeaderView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 150 * [UIScreen zj_screenHScale] + self.inforModel.NAMEHEIGHT + self.inforModel.HEIGHT + 240 * [UIScreen zj_screenHScale]) inforModel:self.inforModel];
    header.delegate = self;
    self.tableView.tableHeaderView = header;

}

#pragma mark - 界面
- (void)layoutUI{
    [self addSubview:self.tableView];
    
   
}

#pragma mark - LPActivityDetailDetailsHeaderViewDelgate
- (void)LPActivityDetailDetailsHeaderViewTap_canYu_tapIndex:(NSInteger)index{
    if (self.delegate) {
        [self.delegate LPACtivityDetailDetailsStudyTabelWithCell:self headerCanYun_tap_index:index];
    }
}

#pragma mark - LPActivityDetailFooterDelegate
- (void)lpActivityFooter_clickButton_index:(NSInteger)index{
    if (self.delegate) {
        [self.delegate LPACtivityDetailDetailsTabelWithCell:self watchMore:YES];
    }

}

/**  学习的代理方法  */
#pragma mark - LPActivityDetailDetailsFooterStudyDelegate
- (void)LPActivityDetailDetailsFooterStudy_tapIndex:(NSInteger)index{
    if (self.delegate) {
        [self.delegate LPACtivityDetailDetailsStudyTabelWithCell:self tap_index:index];
    }

}

- (void)LPActivityDetailDetailsFooterStudy_wactchMore{
    if (self.delegate) {
        [self.delegate LPACtivityDetailDetailsStudyTabelWithCell:self watchMore:YES];
    }

}

#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSouce.count;
    
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = self.dataSouce[section][@"data"];
    if (array.count == 0) {
        return 1;
    }else{
        return array.count;
    }

    
    
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataSouce[indexPath.section][@"title"];
     NSArray *array = self.dataSouce[indexPath.section][@"data"];
    if ([title isEqualToString:@"活动资源"]) {
        return 50 * [UIScreen zj_screenHScale];
    }else{
        if (array.count == 0) {
            return 80 * [UIScreen zj_screenHScale];
        }else{
            return 120 * [UIScreen zj_screenHScale];
        }
    }
    
    
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataSouce[indexPath.section][@"title"];
    NSArray *array = self.dataSouce[indexPath.section][@"data"];

    if ([title isEqualToString:@"活动资源"]) {
        LPActDetailResouceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RECOUCECELL forIndexPath:indexPath];
        cell.model = array[indexPath.row];
        return cell;
    }else{
        
        if (array.count == 0) {
            LPActivityDetailStudyNoDataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NODATA forIndexPath:indexPath];
            cell.title.text = @"暂无往期回顾";
            return cell;
        }else{
            LPActivityDetailsPastTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PASTCELL forIndexPath:indexPath];
            
            cell.model = array[indexPath.row];
            if (indexPath.row == array.count - 1) {
                cell.isLastCell = YES;
            }
            
            return cell;
        }
        
    
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    TenGrayShadowView *backView = [[TenGrayShadowView alloc] initWithFrame:CGRectMake(0, 0, self.width, 40 * [UIScreen zj_screenWScale])];
    backView.backgroundColor = [UIColor themeGrayColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20 * [UIScreen zj_screenWScale], 0, 300, 40 * [UIScreen zj_screenHScale])];
    label.textColor = [UIColor textBlackColor];
    label.font = LYPFONT(17);
    label.text = self.dataSouce[section][@"title"];
    [backView addSubview:label];
    return backView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40 * [UIScreen zj_screenHScale];
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    LPActivityDetailTableFooterView *footer = [[LPActivityDetailTableFooterView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 60 * [UIScreen zj_screenHScale])];
    footer.titles = @[@"查看更多资源"];
    footer.delegate = self;
    if (self.resouceArray.count != 0 && section == 0 ) {
         return footer;
    }else{
        return nil;
    }
   

}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.resouceArray.count != 0 && section == 0 ) {
        return 60 * [UIScreen zj_screenHScale];
    }else{
        return 0;
    }
}

/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   
    if (self.delegate) {
        [self.delegate LPACtivityDetailDetailsTabelWithCell:self index:indexPath.row section:indexPath.section];
    }
        
    
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 200 * [UIScreen zj_screenHScale]) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
        [_tableView registerClass:[LPActDetailResouceTableViewCell class] forCellReuseIdentifier:RECOUCECELL];
        [_tableView registerClass:[LPActivityDetailsPastTableViewCell class] forCellReuseIdentifier:PASTCELL];
        [_tableView registerClass:[LPActivityDetailStudyNoDataTableViewCell class] forCellReuseIdentifier:NODATA];
    }
    return _tableView;
}

- (NSMutableArray *)dataSouce{
    if (!_dataSouce) {
        _dataSouce =  [NSMutableArray array];
    }
    return _dataSouce;
}
@end
